define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Transfer_publish, GoPage, GoDialog, GoPicture, $stateParams,$timeout) {
		//数据模型
		if($scope.transferModel == undefined) $scope.transferModel = new Object();
		$scope.transferModel.goods_id = $stateParams.goods_id;
		$scope.initialize = function() {
			$scope.transferModel.goods_state = "transfer";
			$scope.transferModel.transfer_type = '0';
			$scope.transferModel.transfer_type_array = [{id:1,name:"店铺转让"},{id:2,name:"品牌转让"}];
			$scope.transferModel.transfer_type_select = {id:0,name:"请选择转让类型"};
			$scope.transferModel.name = '';
			$scope.transferModel.bn = '';
			$scope.transferModel.keywords = '';
			$scope.transferModel.brief = '';
			$scope.transferModel.uploads = new Array(); //图片数组
			$scope.transferModel.price = '';
			$scope.transferModel.market_price = '';
			$scope.transferModel.product_bn = '';
			$scope.transferModel.weight = '';
			$scope.transferModel.is_marketable = true;
			$scope.cat_list = new Array();
			$scope.transferModel.cat_list_id = '0';
			$scope.transferModel.cat_list_select = {id:0,cat_name:"请选择自定义分类"};
			$scope.transferModel.property = new Array(); //存放商品属性id和name
			$scope.proArray = new Array(); //通过其获取页面的属性值，没其他作用
		}
		$scope.initialize(); //初始化数据

		//控制数字
		$scope.clearNoNum = function(obj, attr) {
			//先把非数字的都替换掉，除了数字和./^\d{0,}[\.]\d{1,2}$/
			obj[attr] = obj[attr].replace(/[^\d.]/g, "");
			//必须保证第一个为数字而不是.
			obj[attr] = obj[attr].replace(/^\./g, "");
			var end = obj[attr];
			end = end.substr(end.indexOf("."), 3);
			obj[attr] = obj[attr].replace(/\.\d{3,}$/g, "." + end);
			//保证只有出现一个.而没有多个.
			obj[attr] = obj[attr].replace(/\.{2,}/g, ".");
			//保证.只出现一次，而不能出现两次以上
			obj[attr] = obj[attr].replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
		}

		//初始化自定义分类
		$scope.init_data = function() {
			Transfer_publish.get_catlist(function(data) {
				if(data) {
					$scope.cat_list = data.custom_cat;
				}
			});
		}
		$scope.init_data();

		$scope.transfer_sava = function() {
			//		    for (var i=0;i<$scope.propertys.length;i++) {
			//              if($scope.proArray[i]){//如果填了属性值，则要获取该属性的id
			//                  $scope.pro = new Object();//每次都要把该对象push到$scope.productModel.property里面
			//                  $scope.pro.id = document.getElementById("radio["+i+"]").value;//属性id
			//                  $scope.pro.value = $scope.proArray[i];//属性值
			//                  $scope.transferModel.property.push($scope.pro);
			//              }
			//          }
			if($scope.transferModel.transfer_type == 0) {
				GoDialog.toast("请选择转让类型");
			} else if(!$scope.transferModel.name) {
				GoDialog.toast("请输入商品名称");
			} else if($scope.transferModel.price < 0.01){
				GoDialog.toast("请正确填写销售价");
			} else if($scope.transferModel.market_price < 0.01){
				GoDialog.toast("请正确填写市场价");
			} else {
				Transfer_publish.transfer_publish($scope.transferModel.goods_id, $scope.transferModel.goods_state, $scope.transferModel.transfer_type, $scope.transferModel.name, $scope.transferModel.property, $scope.transferModel.bn, $scope.transferModel.keywords, $scope.transferModel.brief, $scope.transferModel.uploads, $scope.transferModel.price, $scope.transferModel.market_price, $scope.transferModel.is_marketable, $scope.transferModel.product_bn, $scope.transferModel.weight, $scope.transferModel.cat_list_id, function(data) {
					if(data.success) {
						GoDialog.toast("保存成功！");
						$scope.initialize(); //初始化数据
						GoPage.go_states("transfer");
					} else {
						GoDialog.toast(data.msg);
						$scope.transferModel.property = new Array();
					}
				});
			}
		}

		$scope.transfer_cancel = function() {
			window.history.go(-1); //返回上一个页面
		}

		//字数统计
		$scope.$watch('transferModel.brief', function(newValue, oldValue) {
			$scope.textLength = newValue.length;
			if($scope.textLength > 70) {
				$scope.transferModel.brief = newValue.substring(0, 70);
			}
		});

		/*===================图片处理=======================*/
		//动态响应
		$scope.doShowFile = function() {
			document.getElementById("upfilebutton").click();
		}

		/*$scope.upload = function(val) {
				var fileList = val.files;
				for(var i = 0; i < val.files.length; i++) {
					var reader = new FileReader();
					reader.readAsDataURL(fileList[i]);
					var file = fileList[i];
					reader.onload = function(e) {
						//图片进行压缩处理
						GoPicture.compress(file,e.target.result, 50, function(base64) {
							$scope.$apply(function() {
								$scope.transferModel.uploads.push(base64);
								val.value = "";
							});
						});
					}
				}
				val.value = "";
			}*/
			//图片上传
                $scope.addStoreImg = function() {
                    GoPicture.upload(function(res) {
                        if(!res.success) {
                            GoDialog.toast(res.msg);
                            return;
                        }
                        console.log(res)
                        $scope.photo = new Object();
                        $scope.photo.url = res.result.image_src; //图片 显示用
                        $scope.photo.id = res.result.image_id; //图片id显示用
                        $scope.transferModel.uploads.push($scope.photo);
                    });
                }
			//图片移除
		$scope.ondelete = function(index) {
			$scope.transferModel.uploads.splice(index, 1);
		};

		//图片放大删除
        $scope.openFileActSheet = function(index) {
            var buttons = new Array();
            buttons.push({ text: '删除图片', style: '' });
            buttons.push({ text: '显示大图', style: '' });

            GoDialog.sheet('图片操作', '取消', buttons, function(bindex) {

                if(bindex == 0) {
                    GoDialog.confirm('删除图片', '是否要删除该图片？', function() {
                        $timeout(function() {
                            $scope.transferModel.uploads.splice(index, 1);
                        }, 1);
                    })
                } else if(bindex == 1) {
                }
                return true;
            });
        }
		/*===================图片处理--end=======================*/
		$scope.doSelectTransfer = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择转让类型",$scope.transferModel.transfer_type_array,$scope.transferModel.transfer_type_select,function(data){
				$scope.transferModel.transfer_type = data.id;
				$scope.transferModel.transfer_type_select = data;
			});
		}
		$scope.doSelectCustom = function(){
			var list = new Array();
			$scope.cat_list.forEach(obj => {
				if(obj.id == undefined)
					obj.id = obj.custom_cat_id
				list.push(obj);
			});
			//id是值 name是显示
			GoDialog.showSelect("请选择自定义分类",list,$scope.transferModel.cat_list_select,function(data){
				$scope.transferModel.cat_list_id = data.custom_cat_id;
				$scope.transferModel.cat_list_select = data;
			});
		}
	}

	controller.$inject = ['$scope', 'Transfer_publish', 'GoPage', 'GoDialog', 'GoPicture', '$stateParams','$timeout'];

	return app.registerController("transfer_publishctrl",controller);
});